<template>
  <div class="hero-edit">
    <h1>{{ id ? "编辑" : "创建" }}英雄</h1>
    <el-form label-width="100px" @submit.native.prevent="save">
      <el-tabs type="border-card" value="basic">
        <!-- 基本信息 -->
        <el-tab-pane label="基本信息" name="basic">
          <!-- 英雄名 -->
          <el-form-item label="名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <!-- 头像 -->
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="(res) => (model.avatar = res.url)"
            >
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- 英雄banner -->
          <el-form-item label="Banner">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="(res) => (model.banner = res.url)"
            >
              <img v-if="model.banner" :src="model.banner" class="banner" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <!-- 称号 -->
          <el-form-item label="称号">
            <el-input v-model="model.title"></el-input>
          </el-form-item>
          <!-- 类型 -->
          <el-form-item label="类型">
            <el-select v-model="model.categories" multiple>
              <el-option
                v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 难度 -->
          <el-form-item label="难度">
            <el-rate
              style="margin-top:0.6rem"
              :max="9"
              show-score
              v-model="model.scores.difficult"
            ></el-rate>
          </el-form-item>
          <!-- 技能 -->
          <el-form-item label="技能">
            <el-rate
              style="margin-top:0.6rem"
              :max="9"
              show-score
              v-model="model.scores.skills"
            ></el-rate>
          </el-form-item>
          <!-- 攻击 -->
          <el-form-item label="攻击">
            <el-rate
              style="margin-top:0.6rem"
              :max="9"
              show-score
              v-model="model.scores.attack"
            ></el-rate>
          </el-form-item>
          <!-- 生存 -->
          <el-form-item label="生存">
            <el-rate
              style="margin-top:0.6rem"
              :max="9"
              show-score
              v-model="model.scores.survive"
            ></el-rate>
          </el-form-item>
          <!-- 顺风出装 -->
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 逆风出装 -->
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 使用技巧 -->
          <el-form-item label="使用技巧">
            <el-input type="textarea" v-model="model.usageTips"></el-input>
          </el-form-item>
          <!-- 对抗技巧 -->
          <el-form-item label="对抗技巧">
            <el-input type="textarea" v-model="model.battleTips"></el-input>
          </el-form-item>
          <!-- 团战思路 -->
          <el-form-item label="团战思路">
            <el-input type="textarea" v-model="model.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <!-- 技能 -->
        <el-tab-pane label="技能" name="skills">
          <el-button @click="model.skills.push({})">
            <i class="el-icon-plus"></i>
            添加技能
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap;margin-top:1rem">
            <el-col :md="12" v-for="(item, i) in model.skills" :key="i">
              <!-- 技能名称 -->
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <!-- 技能图标 -->
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <!-- 冷却 -->
              <el-form-item label="冷却">
                <el-input v-model="item.delay"></el-input>
              </el-form-item>
              <!-- 消耗 -->
              <el-form-item label="消耗">
                <el-input v-model="item.cost"></el-input>
              </el-form-item>
              <!-- 技能描述 -->
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <!-- 技能小提示 -->
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>
              <!-- 技能删除 -->
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.skills.splice(i, 1)"
                >
                  删除
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <!-- 最佳搭档 -->
        <el-tab-pane label="最佳搭档" name="partners">
          <el-button @click="model.partners.push({})">
            <i class="el-icon-plus"></i>
            添加英雄
          </el-button>
          <el-row type="flex" style="flex-wrap:wrap;margin-top:1rem">
            <el-col :md="12" v-for="(item, i) in model.partners" :key="i">
              <!-- 技能名称 -->
              <el-form-item label="英雄">
                <el-select filterable v-model="item.hero">
                  <el-option
                    v-for="hero in heroes"
                    :key="hero._id"
                    :value="hero._id"
                    :label="hero.name"
                  ></el-option>
                </el-select>
              </el-form-item>
              <!-- 英雄描述 -->
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <!-- 删除 -->
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="model.partners.splice(i, 1)"
                >
                  删除
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top:1rem">
        <el-button type="primary" native-type="sumbit">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        name: "",
        avatar: "",
        banner: "",
        scores: {
          difficult: 0,
          skills: 0,
          attack: 0,
          survive: 0,
        },
        skills: [],
        partners: [],
      },
      // 分类
      categories: [],
      // 装备
      items: [],
      //英雄
      heroes: [],
    };
  },
  mounted() {
    this.fetchCategories();
    this.fetchItems();
    this.fetchHeroes();
    this.id && this.fetch();
  },
  methods: {
    //保存修改
    save() {
      let http;
      if (this.id) {
        http = this.$http.put(`rest/heroes/${this.id}`, this.model);
      } else {
        http = this.$http.post("rest/heroes", this.model);
      }
      http.then(() => {
        this.$router.push("/heroes/list");
        this.$message({
          type: "success",
          message: "保存成功",
        });
      });
    },
    //获取
    fetch() {
      this.$http.get(`rest/heroes/${this.id}`).then((res) => {
        this.model = Object.assign({}, this.model, res.data);
      });
    },
    //获取分类
    fetchCategories() {
      this.$http.get(`rest/categories`).then((res) => {
        this.categories = res.data;
      });
    },
    //获取装备
    fetchItems() {
      this.$http.get(`rest/items`).then((res) => {
        this.items = res.data;
      });
    },
    //获取装备
    fetchHeroes() {
      this.$http.get(`rest/heroes`).then((res) => {
        this.heroes = res.data;
      });
    },
  },
};
</script>
